﻿using SimpleConnection_UDP_WIN.extension_class.utils;
using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;


namespace SimpleConnection_UDP_WIN.extension_class.form_ui
{
    public partial class NetMediaForm : Form
    {

        string srcUrl = null;
        string mediaType = null;


        public NetMediaForm(string url,string media_type)
        {
            InitializeComponent();
            srcUrl = url;
            mediaType = media_type;
        }

        private void NetMediaForm_Load(object sender, EventArgs e)
        {


            switch (mediaType)
            {
                case NetworkMediaPlayClass.KEY_IMAGE:
                    playImage(srcUrl);
                    break;
                case NetworkMediaPlayClass.KEY_VIDEO:
                    playVideo(srcUrl);
                    break;
                case NetworkMediaPlayClass.KEY_AUDIO:

                    break;
                default:
                    break;
            }

        }


        private void playVideo(string srcUrl)
        {
            Console.WriteLine("打开视频");


        }

        Image image =null;
        private void playImage(string url)
        {
            Console.WriteLine("打开图片");
            using (WebClient client = new WebClient())
            {
                try
                {
                    // 下载图片
                    byte[] imageData = client.DownloadData(url);

                    // 将下载的图片数据转换为Image对象
                    using (var stream = new System.IO.MemoryStream(imageData))
                    {
                        image = Image.FromStream(stream);

                        // 在PictureBox控件中显示图片
                        pictureBox1.Image = image;
                        pictureBox1.Show();
                        stream.Dispose();
                    }
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载图片失败：" + ex.Message);
                }
                client.Dispose();
            }

        }

        private void NetMediaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //防止内存泄漏，C# 的内存管理像shi
            //图片的资源释放
            if (pictureBox1.Image !=null)
            {
            pictureBox1.Image.Dispose();
            pictureBox1.Dispose();
            pictureBox1 = null; 
            image.Dispose();
            image = null;
            }
            Dispose();


        }
    }
}
